<script setup>
	import {
		ref
	} from 'vue'
	import {
		imgUrl
	} from "../../../utils/global";
	import {
		useOrderStore
	} from '@/store/order';
	import {
		onLoad,
		onPullDownRefresh,
		onShow
	} from "@dcloudio/uni-app";
	import {
		getInstitutionList
	} from "@/api/reserve"
	const orderStore = useOrderStore();

	// 模拟品牌数据
	const brandList = ref();
	const brand_loading = ref(false)
	const getList = () => {
		brand_loading.value = true
		getInstitutionList().then(res => {
			brandList.value = []
			if (orderStore.customerInfo.brandid) {
				brand_list.value = orderStore.customerInfo.brandid.split(",")
				if (typeof brand_list.value == 'string' && brand_list.value) {
					brand_list.value = [brand_list.value]
				}
			}
			if (brand_list.value.length == 0) {
				res.forEach(item => {
					brand_list.value.push(item.brandid)
				})
			}

			res.forEach(item => {
				if (brand_list.value.includes(item.brandid + '')) {
					brandList.value.push(item)
				}
				// brand_list.value.push(item.brandid)
			})
			orderStore.customerInfo.brand_list=brand_list.value

		}).catch(err => {
			brand_loading.value = false
		})
	}
	const brand_list = ref([])
	// 初始化
	onLoad(async (options) => {

		getList()
	});

	// 选择品牌
	const selectBrand = (brand) => {
		if (!brand_list.value.includes(brand.brandid + '')) {
			return
		}
		try {
			// 保存选择的品牌到orderStore
			orderStore.setBrand(brand);
			uni.showToast({
				title: '品牌选择成功',
				icon:"none"
			});
			// 跳转到套餐选择页面
			setTimeout(() => {
				uni.navigateTo({
					url: '/pages/case/select/index'
				});
			}, 1000);

		} catch (error) {
			uni.hideLoading();
			uni.showToast({
				title: '选择失败',
				icon: 'error'
			});
			console.error('选择品牌失败:', error);
		}
	};
</script>

<template>
	<view class="app-brand">
		<view class="app-item" v-for="brand in brandList" :key="brand.id">
			<view class="item-row">
				<image :src="imgUrl(brand.img)" mode="aspectFill" />
				<view class="item-info">
					<text class="item-title">{{ brand.title }}</text>
					<text class="item-desc">{{ brand.introduce }}</text>
					<!--     <view class="item-meta">
                        <text class="item-rating">评分: {{ brand.rating }}</text>
                        <text class="item-packages">{{ brand.packageCount }}个套餐</text>
                    </view> -->
				</view>
			</view>
			<view class="item-button"
				:style="brand_list.includes(brand.brandid+'')?'':'border: 1px solid #e9e9eb;background: #f4f4f5;color: #bcbec2;'"
				@click="selectBrand(brand)">立即预约</view>
		</view>
	</view>
</template>

<style src="./style.scss" scoped />